package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.commpent.mapper.yw.AbstractYwBusinessLoanVoNewMapper;
import com.zjdiepu.www.ibiz.base.IDpBaseDataDictionaryService;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.cw.ICwIncomeExpensesItemService;
import com.zjdiepu.www.ibiz.cw.ICwRemitCarService;
import com.zjdiepu.www.ibiz.his.IHisYwBusinessLoanService;
import com.zjdiepu.www.ibiz.other.IOtherAuditInfoService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanEditService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.ibiz.yw.IYwFeesSubService;
import com.zjdiepu.www.model.base.dto.DpBaseDataDictionary;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.base.ext.DpBaseDataDictionaryExt;
import com.zjdiepu.www.model.cw.response.CwFundItem;
import com.zjdiepu.www.model.his.dto.HisYwBusinessLoan;
import com.zjdiepu.www.model.other.dto.OtherAuditInfo;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoanEdit;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

@Controller
@RequestMapping(value= "YwBusinessLoanEditViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"修改贷款信息"}, produces="text/html;charset=UTF-8")
public class YwBusinessLoanEditViewController extends BaseController{

	@Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private IYwFeesSubService ywFeesSubService;
    @Resource
    private ICwIncomeExpensesItemService cwIncomeExpensesItemService;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    @Resource
    private ICwRemitCarService cwRemitCarService;
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private IDpBaseDataDictionaryService dBaseDataDictionaryService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IYwBusinessLoanEditService ywBusinessLoanEditService;
    @Resource
    private AbstractYwBusinessLoanVoNewMapper hisYwBusinessLoanVoMapper;
    @Resource
    private IHisYwBusinessLoanService hisYwBusinessLoanService;
    @Resource
    private IOtherAuditInfoService otherAuditInfoService;
	
	@ApiOperation(value = "修改贷款信息", response = Boolean.class, httpMethod="GET", tags={"修改贷款信息"})
    @RequestMapping(value = "ywBusinessLoanEditDetail", method = RequestMethod.GET)
    public String ywBusinessLoanEditDetail(Model model, String busiId) {
    	if (StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
//    	CwRemitAdjustBExt cwRemitAdjustBExt= new CwRemitAdjustBExt();
//    	cwRemitAdjustBExt.setBusiId(id);
//    	cwRemitAdjustBExt.setRows(1);
//    	List<CwRemitAdjustB> list=cwRemitAdjustBService.selectByExt(cwRemitAdjustBExt);
    	
    	model.addAttribute("loanBank", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.REMIT_TYPE,"请选择..."));
    	model.addAttribute("carType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_TYPE,"请选择..."));
    	
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(busiId, model);
    	if(ywBusinessDetail != null){
    		viewScopeControllerHelper.preparedYwFee(busiId, ywBusinessDetail.getCustomerId(), model);
    		viewScopeControllerHelper.preparedFundApply(busiId, ywBusinessDetail.getCustomerId(),null,false, model);
    		viewScopeControllerHelper.preparedRemitCar(busiId, ywBusinessDetail.getCustomerId(), model);
    	}
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(busiId,ywBusinessDetail.getCustomerId(), model);
    	if(car != null) {
    		YwBusinessLoanEdit ywBusinessLoanEdit=new YwBusinessLoanEdit();
    		ywBusinessLoanEdit.setBusiId(busiId);
    		YwBusinessLoanEdit ywBusinessLoanEdit1=ywBusinessLoanEditService.selectRefined(ywBusinessLoanEdit);
    		if(ywBusinessLoanEdit1!=null){
    			viewScopeControllerHelper.preparedLoanEdit(busiId, car.getId(), model, car.getCarType());
    		}else{
    			viewScopeControllerHelper.preparedLoan(busiId, car.getId(), model, car.getCarType());
    		}
    	}
    	

//    	viewScopeControllerHelper.preparedCarModel(model);
//    	获取费用数据字典=====================
    	DpBaseDataDictionaryExt dpBaseDataDictionaryExt = new DpBaseDataDictionaryExt();
    	dpBaseDataDictionaryExt.setBaseWhere();
    	dpBaseDataDictionaryExt.withOutPage();
    	dpBaseDataDictionaryExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	dpBaseDataDictionaryExt.setAddedValue1("01"); //启用的明细
    	dpBaseDataDictionaryExt.setOrderSql("`VALUE`");
//		只展示附加值为1的数据
    	List<DpBaseDataDictionary> dpBaseDataDictionarys = dBaseDataDictionaryService.selectByExt(dpBaseDataDictionaryExt);
    	
    	DpBaseDataDictionaryExt depositItemExt = new DpBaseDataDictionaryExt();
    	depositItemExt.setBaseWhere();
    	depositItemExt.setRows(1);
    	depositItemExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	depositItemExt.setValue("35");
    	List<DpBaseDataDictionary> depositDataDictionaryItems = dBaseDataDictionaryService.selectByExt(depositItemExt);
    	if(CollectionUtils.isNotEmpty(depositDataDictionaryItems)) {
    		dpBaseDataDictionarys.add(depositDataDictionaryItems.get(0));
    	} else {
    		DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    		dpBaseDataDictionary.setValue("35");
    		dpBaseDataDictionary.setName("征信押金");
    		dpBaseDataDictionary.setParentValue(DataDicType.FUND_ITEM.getCode());
    		dpBaseDataDictionarys.add(dpBaseDataDictionary);
    	}
    	depositItemExt.setBaseWhere();
    	depositItemExt.setRows(1);
    	depositItemExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	depositItemExt.setValue("01");
    	List<DpBaseDataDictionary> depositDataDictionaryItems_ = dBaseDataDictionaryService.selectByExt(depositItemExt);
    	if(CollectionUtils.isNotEmpty(depositDataDictionaryItems_)) {
    		dpBaseDataDictionarys.add(depositDataDictionaryItems_.get(0));
    	} else {
    		DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    		dpBaseDataDictionary.setValue("01");
    		dpBaseDataDictionary.setName("按揭服务费");
    		dpBaseDataDictionary.setParentValue(DataDicType.FUND_ITEM.getCode());
    		dpBaseDataDictionarys.add(dpBaseDataDictionary);
    	}
//    	获取费用数据字典=====================
    	
    	
//    	==================应收获取============================
    	List<Map<String, Double>> fees = ywFeesSubService.countReceiveByBusiId(busiId, null);
//    	==================应收获取============================
    	
//    	==================已收获取============================   	
    	List<String> exludeKeys = Arrays.asList(new String[]{"01", "35"});
    	List<CwFundItem> fundItems = CollectionInitUtil.emptyList();
    	List<CwFundItem> fundItems_ = CollectionInitUtil.emptyList();
    	List<Map<String, Double>> remits = cwIncomeExpensesItemService.countReceiveByBusiId(busiId, null);
    	if(CollectionUtils.isNotEmpty(dpBaseDataDictionarys)) {
    		for(DpBaseDataDictionary dpBaseDataDictionary : dpBaseDataDictionarys) {
    			CwFundItem cwFundItem = new CwFundItem();
    			String key = dpBaseDataDictionary.getValue();
    			cwFundItem.setKey(key);
    			cwFundItem.setName(dpBaseDataDictionary.getName());
    			cwFundItem.setFeeCount(ViewScopeControllerHelper.getItem(fees, key));
    			cwFundItem.setRemitCount(ViewScopeControllerHelper.getItem(remits, key));
    			if(exludeKeys.contains(key)) {
    				fundItems_.add(cwFundItem);
    			} else {
    				fundItems.add(cwFundItem);
    			}
    		}
    	}
//    	==================已收获取============================  
    	model.addAttribute("fundItems", fundItems);
    	model.addAttribute("fundItems_", fundItems_);
    	
    	CwFundItem cwFundItem = new CwFundItem();
    	cwFundItem.setFeeCount(ViewScopeControllerHelper.countItems(fees, null));
    	cwFundItem.setRemitCount(ViewScopeControllerHelper.countItems(remits, null));
    	model.addAttribute("fundTotal", cwFundItem);
    	
    	String history = cwRemitCarService.queryHistoryByBusiId(busiId);
    	model.addAttribute("history", StringUtils.isBlank(history)?"-":(history.startsWith(",")?history.substring(1):history));
    	
    	SysUser sysUser =(SysUser)getCurrentUser();
    	viewScopeControllerHelper.preparedAllocation(sysUser.getOrgId(), model);
    	
    	YwBusiness ywBusiness=ywBusinessService.selectById(busiId);
    	model.addAttribute("ywBusiness",ywBusiness);
    	model.addAttribute("loanMonthType",cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LOAN_MONTH,"请选择..."));
    	return "ywShouLi/sqUpdatedaikuan";
    }
	
	
	@ApiOperation(value = "修改贷款历史信息", response = Boolean.class, httpMethod="GET", tags={"修改贷款历史信息"})
    @RequestMapping(value = "ywBusinessLoanEditHistoryDetail", method = RequestMethod.GET)
    public String ywBusinessLoanEditHistoryDetail(Model model, String busiId, String loanEditId) {
    	if (StringUtils.isBlank(busiId) || StringUtils.isBlank(loanEditId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(busiId, model);
    	if(null != ywBusinessDetail) {
    		YwBusinessCar car = viewScopeControllerHelper.preparedCar(busiId,ywBusinessDetail.getCustomerId(), model);
    		if(null != car) {
    			viewScopeControllerHelper.preparedLoan(busiId, car.getId(), model, car.getCarType());

    			YwBusinessLoanEdit ywBusinessLoanEdit = ywBusinessLoanEditService.selectById(loanEditId);
    	    	if(null != ywBusinessLoanEdit) {
    	    		model.addAttribute("shenqin", ywBusinessLoanEdit.getCreatorName());
    	    		model.addAttribute("shenqinDate", DateUtil.dateTo8String(new Date(ywBusinessLoanEdit.getCreated() * 1000L)));
//    	    		直接取值
    	    		hisYwBusinessLoanVoMapper.mapper(ywBusinessLoanEdit); 
    	    		ywBusinessLoanEdit.setCarType(car.getCarType());
    	    		model.addAttribute("ywBusinessLoanEdit", ywBusinessLoanEdit);
    	    		
    	    		OtherAuditInfo otherAuditInfoParam = new OtherAuditInfo();
    	        	otherAuditInfoParam.setBusiId(busiId);
    	        	otherAuditInfoParam.setDataId(loanEditId);
    	        	
    	        	otherAuditInfoParam.setModeId(ModeIDType.XGDKSHY.getCode());
    	        	OtherAuditInfo otherAuditInfo1 = otherAuditInfoService.selectRefined(otherAuditInfoParam);
    	        	if(null != otherAuditInfo1) {
    	        		model.addAttribute("shenheRen1", otherAuditInfo1.getCreatorName());
    	        		model.addAttribute("shenheRen1JieGuo", cachedDictionaryService.getName(otherAuditInfo1.getAuditStatus(), DataDicType.AUDITLISTSTATUS));
    	        		model.addAttribute("shenheDate1", DateUtil.dateTo8String(new Date(otherAuditInfo1.getCreated() * 1000L)));
    	        	}
    	        	
    	        	otherAuditInfoParam.setModeId(ModeIDType.XGDKSHE.getCode());
    	        	OtherAuditInfo otherAuditInfo2 = otherAuditInfoService.selectRefined(otherAuditInfoParam);
    	        	if(null != otherAuditInfo2) {
    	        		model.addAttribute("shenheRen2", otherAuditInfo2.getCreatorName());
    	        		model.addAttribute("shenheRen2JieGuo", cachedDictionaryService.getName(otherAuditInfo2.getAuditStatus(), DataDicType.AUDITLISTSTATUS));
    	        		model.addAttribute("shenheDate2", DateUtil.dateTo8String(new Date(otherAuditInfo2.getCreated() * 1000L)));
    	        	}
    	    		
    	    		String hisLoanId = ywBusinessLoanEdit.getHisLoanId();
    	    		if(StringUtils.isNotBlank(hisLoanId)) {
    	    			HisYwBusinessLoan hisYwBusinessLoan = hisYwBusinessLoanService.selectById(hisLoanId);
    	    			if(null != hisYwBusinessLoan) {
//    	        			直接取值
    	    				hisYwBusinessLoanVoMapper.mapper(hisYwBusinessLoan);
    	    				hisYwBusinessLoan.setCarType(car.getCarType());
    	    				model.addAttribute("hisYwBusinessLoan", hisYwBusinessLoan);
    	    				
    	    			}
    	    		}
    	    	}
    		}
    		viewScopeControllerHelper.preparedYwFee(busiId, ywBusinessDetail.getCustomerId(), model);
    	}
    	
//    	获取费用数据字典=====================
    	DpBaseDataDictionaryExt dpBaseDataDictionaryExt = new DpBaseDataDictionaryExt();
    	dpBaseDataDictionaryExt.setBaseWhere();
    	dpBaseDataDictionaryExt.withOutPage();
    	dpBaseDataDictionaryExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	dpBaseDataDictionaryExt.setAddedValue1("01"); //启用的明细
    	dpBaseDataDictionaryExt.setOrderSql("`VALUE`");
//		只展示附加值为1的数据
    	List<DpBaseDataDictionary> dpBaseDataDictionarys = dBaseDataDictionaryService.selectByExt(dpBaseDataDictionaryExt);
    	
    	DpBaseDataDictionaryExt depositItemExt = new DpBaseDataDictionaryExt();
    	depositItemExt.setBaseWhere();
    	depositItemExt.setRows(1);
    	depositItemExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	depositItemExt.setValue("35");
    	List<DpBaseDataDictionary> depositDataDictionaryItems = dBaseDataDictionaryService.selectByExt(depositItemExt);
    	if(CollectionUtils.isNotEmpty(depositDataDictionaryItems)) {
    		dpBaseDataDictionarys.add(depositDataDictionaryItems.get(0));
    	} else {
    		DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    		dpBaseDataDictionary.setValue("35");
    		dpBaseDataDictionary.setName("征信押金");
    		dpBaseDataDictionary.setParentValue(DataDicType.FUND_ITEM.getCode());
    		dpBaseDataDictionarys.add(dpBaseDataDictionary);
    	}
    	depositItemExt.setBaseWhere();
    	depositItemExt.setRows(1);
    	depositItemExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	depositItemExt.setValue("01");
    	List<DpBaseDataDictionary> depositDataDictionaryItems_ = dBaseDataDictionaryService.selectByExt(depositItemExt);
    	if(CollectionUtils.isNotEmpty(depositDataDictionaryItems_)) {
    		dpBaseDataDictionarys.add(depositDataDictionaryItems_.get(0));
    	} else {
    		DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    		dpBaseDataDictionary.setValue("01");
    		dpBaseDataDictionary.setName("按揭服务费");
    		dpBaseDataDictionary.setParentValue(DataDicType.FUND_ITEM.getCode());
    		dpBaseDataDictionarys.add(dpBaseDataDictionary);
    	}
//    	获取费用数据字典=====================
    	
    	
//    	==================应收获取============================
    	List<Map<String, Double>> fees = ywFeesSubService.countReceiveByBusiId(busiId, null);
//    	==================应收获取============================
    	
//    	==================已收获取============================   	
    	List<String> exludeKeys = Arrays.asList(new String[]{"01", "35"});
    	List<CwFundItem> fundItems = CollectionInitUtil.emptyList();
    	List<CwFundItem> fundItems_ = CollectionInitUtil.emptyList();
    	List<Map<String, Double>> remits = cwIncomeExpensesItemService.countReceiveByBusiId(busiId, null);
    	if(CollectionUtils.isNotEmpty(dpBaseDataDictionarys)) {
    		for(DpBaseDataDictionary dpBaseDataDictionary : dpBaseDataDictionarys) {
    			CwFundItem cwFundItem = new CwFundItem();
    			String key = dpBaseDataDictionary.getValue();
    			cwFundItem.setKey(key);
    			cwFundItem.setName(dpBaseDataDictionary.getName());
    			cwFundItem.setFeeCount(ViewScopeControllerHelper.getItem(fees, key));
    			cwFundItem.setRemitCount(ViewScopeControllerHelper.getItem(remits, key));
    			if(exludeKeys.contains(key)) {
    				fundItems_.add(cwFundItem);
    			} else {
    				fundItems.add(cwFundItem);
    			}
    		}
    	}
//    	==================已收获取============================  
    	model.addAttribute("fundItems", fundItems);
    	model.addAttribute("fundItems_", fundItems_);
    	
    	CwFundItem cwFundItem = new CwFundItem();
    	cwFundItem.setFeeCount(ViewScopeControllerHelper.countItems(fees, null));
    	cwFundItem.setRemitCount(ViewScopeControllerHelper.countItems(remits, null));
    	model.addAttribute("fundTotal", cwFundItem);
    	return "system/kehubiao/UpdatedaikuanHistoryDetail";
    }
}
